package edu.yctc.patterns.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;

import edu.yctc.patterns.cache.BlogCache;
import edu.yctc.patterns.constant.ResultCode;
import edu.yctc.patterns.context.InitialContext;
import edu.yctc.patterns.dao.BlogDAO;
import edu.yctc.patterns.entity.BlogDO;
import edu.yctc.patterns.entity.ResultDO;
import edu.yctc.patterns.service.BlogIService;

/**
 * @className BlogServiceImpl
 * @description blogService
 * @author zyj
 * @date 2019年12月12日
 *
 */
@ComponentScan({"edu.yctc.patterns.dao"})
@Service("blogService")
public class BlogServiceImpl implements BlogIService {

    private final static Logger LOG = LoggerFactory.getLogger("blogLogger");

    private final static String BLOGDAO_JNDI_NAME = "BlogDAO";

    @Autowired
    private InitialContext initialContext;

    @Override
    public ResultDO<BlogDO> getBlogDOByTitle(String title) {
        if (StringUtils.isBlank(title)) {
            LOG.error("get blogDO by title fail, parameter invalid, title={}", title);
            return new ResultDO<BlogDO>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID, null);
        }
        try {
            // 先去缓存查找
            BlogDO blogDO = null;
            blogDO = BlogCache.getBlogDOByTitle(title);
            if (blogDO != null) {
                LOG.info("get blogDO by title from cache success, title={}, blogDO={}", title, blogDO);
                return new ResultDO<BlogDO>(true, ResultCode.FROM_CACHE, ResultCode.MSG_FROM_CACHE, blogDO);
            }
            // 缓存中没有, 从数据库中查找
            // 初始上下文
            BlogDAO blogDAO = (BlogDAO)initialContext.lookup(BLOGDAO_JNDI_NAME);
            blogDO = blogDAO.getBlogDOByTitle(title);
            if (blogDO != null) {
                LOG.info("get blogDO by title from DB success, title={}, blogDO={}", title, blogDO);
                // 加入缓存
                BlogCache.addBlogDO(title, blogDO);
                return new ResultDO<BlogDO>(true, ResultCode.FROM_DB, ResultCode.MSG_FROM_DB, blogDO);
            }
            LOG.error("get blogDO by title fail, no such blog, title={}", title);
            return new ResultDO<BlogDO>(false, ResultCode.NO_SUCH_BLOG, ResultCode.MSG_NO_SUCH_BLOG, null);
        } catch (Exception e) {
            LOG.error("get blogDO by title error, title={}", title, e);
            return new ResultDO<BlogDO>(false, ResultCode.ERROR_SYSTEM_EXCEPTION, ResultCode.MSG_ERROR_SYSTEM_EXCEPTION,
                null);
        }
    }

    @Override
    public ResultDO<Void> refreshBlogCache(String title) {
        if (StringUtils.isBlank(title)) {
            LOG.error("refresh blog cache fail, parameter invalid, title={}", title);
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        try {
            // 直接删除缓存
            BlogCache.deleteBlogDO(title);
            LOG.info("refresh blog cache success, title={}", title);
            return new ResultDO<>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
        } catch (Exception e) {
            LOG.error("refresh blog cache error, title={}", title, e);
            return new ResultDO<>(false, ResultCode.ERROR_SYSTEM_EXCEPTION, ResultCode.MSG_ERROR_SYSTEM_EXCEPTION);
        }
    }

}
